package com.zjzaki.thingsboard.ui.main;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.github.mikephil.charting.charts.LineChart;
import com.zjzaki.thingsboard.databinding.FragmentMainBinding;
import com.zjzaki.thingsboard.ui.CreationChart;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    private PageViewModel pageViewModel;
    private FragmentMainBinding binding;

    public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static PlaceholderFragment newInstance(int index, String deviceId, String deviceName, String token) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        bundle.putString("deviceId", deviceId);
        bundle.putString("deviceName", deviceName);
        bundle.putString("token", token);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageViewModel = new ViewModelProvider(this).get(PageViewModel.class);
        int index = 1;
        String deviceId = "";
        String deviceName = "";
        String token = "";
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_SECTION_NUMBER);
            deviceId = getArguments().getString("deviceId");
            deviceName = getArguments().getString("deviceName");
            token = getArguments().getString("token");
        }
        //pageViewModel.setIndex(index);
        pageViewModel.setInfo(index, deviceId, deviceName, token);
    }

    /**
     * 创建视图
     */
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = FragmentMainBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        //final TextView textView = bind   ing.sectionLabel;
        LineChart lineChart = binding.historialLineChart;
        //pageViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
        //    @Override
        //    public void onChanged(@Nullable String s) {
        //        //textView.setText(s);
        //       // lineChart.setData();
        //    }
        //});

        pageViewModel.getData().observe(getViewLifecycleOwner(), new Observer<Map<Long, Float>>() {

            @Override
            public void onChanged(Map<Long, Float> longFloatMap) {
                //创建图表
                CreationChart creationChart = new CreationChart(lineChart);
                //调用初始化方法
                creationChart.init();
                //判断map是否为空
                if (longFloatMap != null && longFloatMap.size() != 0) {
                    for (Long aLong : longFloatMap.keySet()) {
                        creationChart.addData(aLong,longFloatMap.get(aLong));
                    }
                }
            }
        });
        return root;
    }

    /**
     * 销毁页面
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}